/**
 * Created by red on 2017/4/30.
 */
import parse from '@/utils/parse';
/**
 * 文章管理
 * @type {Object}
 */
export default [
  {
    name: '文章列表',
    method: 'articleList',
    excute: ({ data, fn, errFn } = {}) => {
      parse.query(
        'books',
        query => {
          query.equalTo('class', data.class);
          query.count().then(res => data.page.hasMore = res > (data.currentPage * 10 + 10) ? true : false, err => data.page.hasMore = false);
          query.skip(data.currentPage * 10);
          query.limit(10);
        }
      ).filter(x => x.type && x.type === 'result')
        .map(x => {
          console.log('x.result::', x.result);
          return x.result.map(item => ({
            id: item.id,
            name: item.get('name'),
            describe: item.get('describe'),
            cover: item.get('cover')._url,
            price: item.get('price'),
            chapter_count: item.get('chapter_count'),
            author: item.get('author'),
            word_count: item.get('word_count'),
            grade_type: item.get('grade_type'),
            gold_coin: item.get('gold_coin')
          }));
        })
        .subscribe(fn, errFn);
    }
  },
  {
    name: '年级书架',
    method: 'queryBooksListByClass',
    excute: ({ data, fn, errFn } = {}) => {
      console.log("年级书架", data);
      parse.query(
        'books',
        query => {
          query.equalTo('class', data.class_);
          query.count().then(res => data.page.hasMore = res > (data.currentPage * 10 + 10) ? true : false, err => data.page.hasMore = false);
          query.skip(data.currentPage * 10);
          query.limit(10);
        }
      ).filter(x => x.type && x.type === 'result')
        .map(x => {
          return x.result.map(item => ({
            id: item.id,
            name: item.get('name'),
            describe: item.get('describe'),
            cover: item.get('cover'),
            price: item.get('price'),
            chapter_count: item.get('chapter_count'),
            author: item.get('author'),
            word_count: item.get('word_count'),
            grade_type: item.get('grade_type'),
            read_chapter_count: item.get('read_chapter_count'),
            read_word_count: item.get('read_word_count'),
          }));
        })
        .subscribe(fn, errFn);
    }
  },
  {
    name: '我的文章列表',
    method: 'myArticleList',
    excute: ({ data, fn, errFn } = {}) => {
      parse.query(
        'student_books',
        query => {
          query.equalTo('student_id', data.student_id);
          query.count().then(res => data.page.hasMore = res > (data.currentPage * 10 + 10) ? true : false, err => data.page.hasMore = false);
          query.skip(data.currentPage * 10);
          query.limit(10);
        }
      ).filter(x => x.type && x.type === 'result')
        .map(x => {
          return x.result.map(item => ({
            id: item.id,
            name: item.get('name'),
            describe: item.get('describe'),
            cover: item.get('cover'),
            price: item.get('price'),
            chapter_count: item.get('chapter_count'),
            author: item.get('author'),
            word_count: item.get('word_count'),
            grade_type: item.get('grade_type'),
            read_chapter_count: item.get('read_chapter_count'),
            read_word_count: item.get('read_word_count'),
          }));
        })
        .subscribe(fn, errFn);
    }
  },
  {
    name: '我获得的文章金币',
    method: 'student_book_gold',
    excute: ({ data, fn, errFn } = {}) => {
      parse.query(
        'student_books',
        query => {
          query.equalTo('student_id', data);
        }
      ).filter(x => x.type && x.type === 'result')
        .map(x => {
          return x.result.map(item => ({
            id: item.id,
            book_id: item.get('book_id'),
            gold: item.get('get_gold_coin')
          }));
        })
        .subscribe(fn, errFn);
    }
  },
  {
    name: '我获得的章节金币',
    method: 'student_chapter_gold',
    excute: ({ data, fn, errFn } = {}) => {
      parse.query(
        'student_chapters',
        query => {
          query.equalTo('student_id', data.student_id);
          query.equalTo('book_id', data.book_id);
        }
      ).filter(x => x.type && x.type === 'result')
        .map(x => {
          return x.result.map(item => ({
            id: item.id,
            chapter_id: item.get('chapter_id'),
            get_gold_coin: item.get('get_gold_coin')
          }));
        })
        .subscribe(fn, errFn);
    }
  },
  {
    name: '文章目录列表',
    method: 'chapterList',
    excute: ({ data, fn, errFn } = {}) => {
      parse.query('chapters', query => query.equalTo('book_id', data).ascending('chapter_index'))
        .filter(x => x.type && x.type === 'result')
        .map(x => {
          console.log('x.result::', x.result);
          return x.result.map(item => ({
            id: item.id,
            title: item.get('title'),
            word_count: item.get('word_count'),
            gold_coin: item.get('gold_coin'),
          }));
        })
        .subscribe(fn, errFn);
    }
  },
  {
    name: '查询章节',
    method: 'chapterDetail',
    excute: ({ data, fn, errFn } = {}) => {
      parse.query('chapters', query => query.equalTo('objectId', data))
        .filter(x => x.type && x.type === 'result')
        .map(x => {
          console.log('x.result::', x.result);
          return x.result.map(item => ({
            id: item.id,
            book_id: item.get('book_id'),
            title: item.get('title'),
            word_count: item.get('word_count'),
            content: item.get('content'),
            chapter_index: item.get('chapter_index'),
          }));
        })
        .subscribe(next => fn(next[0]));
    }
  },
  {
    name: '查询文章',
    method: 'findArticle',
    excute: ({ data, fn, errFn } = {}) => {
      parse.query('books', query => query.equalTo('objectId', data))
        .filter(x => x.type && x.type === 'result')
        .map(x => {
          return x.result.map(item => ({
            id: item.id,
            name: item.get('name'),
            describe: item.get('describe'),
            cover: item.get('cover')._url,
            price: item.get('price'),
            chapter_count: item.get('chapter_count'),
            author: item.get('author'),
            word_count: item.get('word_count'),
            grade_type: item.get('grade_type'),
            gold_coin: item.get('gold_coin'),
          }));
        })
        .subscribe(next => fn(next[0]));
    }
  },
  {
    name: '查询我的文章',
    method: 'findMyArticle',
    excute: ({ data, fn, errFn } = {}) => {
      parse.query('student_books', query => query.equalTo('objectId', data))
        .filter(x => x.type && x.type === 'result')
        .map(x => {
          return x.result.map(item => ({
            id: item.id,
            book_id: item.get('book_id'),
            read_chapter_count: item.get('read_chapter_count'),
            read_words_count: item.get('read_words_count'),
            name: item.get('name'),
            describe: item.get('describe'),
            cover: item.get('cover'),
            price: item.get('price'),
            chapter_count: item.get('chapter_count'),
            author: item.get('author'),
            word_count: item.get('word_count'),
            chapter_index: item.get('chapter_index'),//正在读的章节下标
          }));
        })
        .subscribe(next => fn(next[0]));
    }
  },
  {
    name: '修改文章',
    method: 'update_student_book',
    excute: ({ data, fn, errFn } = {}) => {
      parse.query('student_books', query => query.equalTo('book_id', data.book_id).equalTo('student_id', data.student_id))
        .filter(x => x.type && x.type === 'result')
        .map(x => x.result[0])
        .subscribe(res => {
          parse.update(res, {
            ...data.data,
            read_words_count: res.get('read_words_count') + (data.data.read_words_count || 0)
          }).subscribe(fn, errFn);
        });
    }
  },
  {
    name: '添加书单',
    method: 'save_student_book',
    excute: ({ data, fn, errFn } = {}) => {
      parse.create('student_books', data).subscribe(fn, errFn);
    }
  },
  {
    name: '添加学生阅读章节数',
    method: 'save_student_capter_index',
    excute: ({ data, fn, errFn } = {}) => {
      parse.create('student_capter_index', data).subscribe(fn, errFn);
    }
  },
  {
    name: '检查是否存在书单',
    method: 'check_student_book_status',
    excute: ({ data, fn, errFn } = {}) => {
      parse.query('student_books', query => query.equalTo('book_id', data.book_id).equalTo('student_id', data.student_id))
        .filter(x => x.type && x.type === 'result')
        .map(x => ({ get_gold_coin: !!x.result.length ? x.result[0].get('get_gold_coin') : 0, state: !!x.result.length, chapter_index: !!x.result.length ? x.result[0].get('chapter_index') : 0 })).subscribe(fn, errFn);
    }
  },
  {
    name: '查询是否存在已读章节',
    method: 'chapterStatus',
    excute: ({ data, fn, errFn } = {}) => {
      parse.query('student_chapters', query => query.equalTo('chapter_id', data.chapter_id).equalTo('student_id', data.student_id))
        .filter(x => x.type && x.type === 'result')
        .map(x => ({ read_state: !!x.result.length ? x.result[0].get('read_state') : 'unread', finish_question: !!x.result.length ? x.result[0].get('finish_question') : false })).subscribe(fn, errFn);
    }
  },
  {
    name: '查询我的章节',
    method: 'find_student_chapter',
    excute: ({ data, fn, errFn } = {}) => {
      parse.query('student_chapters', query => query.equalTo('chapter_id', data.chapter_id).equalTo('student_id', data.student_id))
        .filter(x => x.type && x.type === 'result')
        .map(x => {
          return x.result.map(item => ({
            id: item.id,
            book_id: item.get('book_id'),
            chapter_id: item.get('chapter_id'),
            get_gold_coin: item.get('get_gold_coin'),
          }));
        })
        .subscribe(next => fn(next[0]));
    }
  },
  {
    name: '添加阅读章节',
    method: 'save_student_chapter',
    excute: ({ data, fn, errFn } = {}) => {
      parse.create('student_chapters', data).subscribe(fn, errFn);
    }
  },
  {
    name: '修改已读章节',
    method: 'update_student_chapter',
    excute: ({ data, fn, errFn } = {}) => {
      parse.query('student_chapters', query => query.equalTo('chapter_id', data.chapter_id).equalTo('student_id', data.student_id))
        .filter(x => x.type && x.type === 'result')
        .map(x => x.result[0])
        .subscribe(res => {
          parse.update(res, data.data).subscribe(fn, errFn);
        });
    }
  },
  {
    name: '修改已读文章所获金币数',
    method: 'update_student_books_gold',
    excute: ({ data, fn, errFn } = {}) => {
      parse.query('student_books', query => query.equalTo('book_id', data.book_id).equalTo('student_id', data.student_id))
        .filter(x => x.type && x.type === 'result')
        .map(x => x.result[0])
        .subscribe(res => {
          parse.update(res, { get_gold_coin: (res.get('get_gold_coin') || 0) + data.get_gold_coin }).subscribe(fn, errFn);
        });
    }
  },
];